package com.sirdc.modules.bnp.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.bnp.filter.SysNodeScheduleFilter;
import com.sirdc.modules.bnp.filter.SysProjectFilter;
import com.sirdc.modules.bnp.service.SysNodeScheduleService;
import com.sirdc.modules.bnp.service.SysNodeTempleteService;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.StringUtils;

import net.huashitong.Project.service.ProIndustryTypeService;
import net.huashitong.node.entity.NodeSchedules;
import net.huashitong.node.entity.NodeTemplet;

@Controller
@RequestMapping(value = "/bnp/project")
public class SysProjectController extends JsonBaseController {

	@Autowired
	private SysNodeTempleteService temService;
	@Autowired
	private ProIndustryTypeService proIndusService;

	@Autowired
	private SysNodeScheduleService scheduleService;
	@Autowired
	private SysUserService sysUserService;
	@Override
	protected String getView(String view) {
		return "/bnp/project/"+view;
	}
	@RequestMapping(method = RequestMethod.GET)
	public String initProjectPage(){
		return getView("query");
	}

	@RequestMapping("/filter")
	public String filter(@ModelAttribute SysProjectFilter filter) {
		return getView("filter");
	}
	@ResponseBody
	@RequestMapping("/setTemplete")
	public Message setTemplete(@RequestParam String sysId){
		try {
			List<NodeTemplet> templets = temService.query();
			for(NodeTemplet templet: templets){
				NodeSchedules nodes = new NodeSchedules();
				nodes.setNodeId(templet.getNodeId());
				nodes.setNodeName(templet.getNodeName());
				nodes.setPreNodeId(templet.getPreNodeId());
				nodes.setProjectId(sysId);
				nodes.setResDept(templet.getResDept());
				nodes.setResUser(templet.getResUser());
				nodes.setType(templet.getType());
				nodes.setResUserPhone(templet.getResUserPhone());
				scheduleService.save(nodes);
			}
			return coverMessage("200", "设置成功");
		} catch (Exception e) {
			e.printStackTrace();
			return coverMessage("500", "设置失败");
		}
		
	}


	@ResponseBody
	@RequestMapping(value = "getProjectNodeScheduleData")
	public JqGrid getProjectNodeSchedule(@ModelAttribute SysNodeScheduleFilter filter,HttpServletRequest request){
		String proId = request.getParameter("proId");
		List<NodeSchedules> list = scheduleService.query(filter,proId);
		return coverJqGrid(filter, list);
	}
	@RequestMapping(value = "setResUserView")
	public String setRseUserView(HttpServletRequest request,Model model){
		String sysId = request.getParameter("sysId");
		String projectId = request.getParameter("projectId");
		model.addAttribute("sysId", sysId);
		model.addAttribute("projectId", projectId);
		NodeSchedules schedules = scheduleService.getByLockId(sysId);
		List<SysUser> zeList=sysUserService.queryPosition("责任人");
		List<SysUser> jbList=sysUserService.queryPosition("经办人");
		model.addAttribute("zeList",zeList);
		model.addAttribute("jbList",jbList);
		model.addAttribute("nodes", schedules);
		
		return getView("setView");
	}
	@ResponseBody
	@RequestMapping(value = "setNodeInfo")
	public Message setNodeInfo(HttpServletRequest request){
		try {
			String sysId = request.getParameter("sysId");
			/*String resUser = request.getParameter("resUser");
			String resUserPhone = request.getParameter("resUserPhone");
			String operatorUser = request.getParameter("operatorUser");
			String operatorPhone = request.getParameter("operatorPhone");*/
			String resUserId = request.getParameter("resUserId");
			String operatorUserId = request.getParameter("operatorUserId");
			String planTime = request.getParameter("planTime");
			String planBeginDate = request.getParameter("planBeginDate");
			String planFinishDate = request.getParameter("planFinishDate");
			String factTime = request.getParameter("factTime");
			String factBeginDate = request.getParameter("factBeginDate");
			String factFinishDate = request.getParameter("factFinishDate");
			String status = request.getParameter("status");
			NodeSchedules schedules = scheduleService.getByLockId(sysId);
            if(StringUtils.isNotBlank(resUserId)){
            	SysUser resUser=sysUserService.getById(resUserId);
            	schedules.setResUser(resUser.getName());
    			schedules.setResUserPhone(resUser.getMobile());
    			schedules.setResUserId(resUserId);
            }
		    if(StringUtils.isNotBlank(operatorUserId)){
		    	SysUser operatorUser=sysUserService.getById(operatorUserId);
				schedules.setOperatorPhone(operatorUser.getMobile());
				schedules.setOperatorUser(operatorUser.getName());
				schedules.setOperatorUserId(operatorUserId);
		    }
			schedules.setPlanTime(planTime);
			schedules.setPlanBeginDate(planBeginDate);
			schedules.setPlanFinishDate(planFinishDate);
			schedules.setFactTime(factTime);
			schedules.setFactBeginDate(factBeginDate);
			schedules.setFactFinishDate(factFinishDate);
			
			schedules.setStatus(status);
			scheduleService.update(schedules);
			return coverMessage("200","设置成功");
		} catch (Exception e) {
			e.printStackTrace();
			return coverMessage("500","设置错误");
		}
	}

}
